﻿let $have_choose_table;
let $un_choose_table;

window.onload = function () {
  initHaveChooseCourse();
  initUnChooseCrouse();
}

var un_choose_testData = {
  total: "2",
  rows: [
    {
      cour_id: "1",				//课程号
      teache_name: "李华",				//教师名称
      title: "机组",						//课程标题
      credit: "2",						//课程学分
      hour: "48",						//课程学时
      attribute: "1",					//课程性质（公选or专选
    }, {
      cour_id: "2",				//课程号
      teache_name: "张华",				//教师名称
      title: "C++",						//课程标题
      credit: "2",						//课程学分
      hour: "48",						//课程学时
      attribute: "1",					//课程性质（公选or专选
    },
  ]
}

var have_choose_testData = {
  total: "2",
  rows: [
    {
      cour_id: "1",				//课程号
      teache_name: "李华",				//教师名称
      title: "Java",						//课程标题
      credit: "2",						//课程学分
      hour: "48",						//课程学时
      attribute: "1",					//课程性质（公选or专选
    }, {
      cour_id: "2",				//课程号
      teache_name: "ASP程序设计",				//教师名称
      title: "C++",						//课程标题
      credit: "2",						//课程学分
      hour: "48",						//课程学时
      attribute: "1",					//课程性质（公选or专选
    },
  ]
}

//生成未选择课程表格
function initUnChooseCrouse() {
  $have_choose_table = $('#student-unchoose-warp').bootstrapTable({
    url:"cotro/student/unChooseCourse.ashx",
    method:"post",

    //data: un_choose_testData.rows,
    search: true,
    sidePagination: "client",
    pageList: [5, 25, 50, 100],
    pagination: true,
    height: 400,
    singleSelect: false,
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    queryParams: function (params) {
      //params.stu_id = 10012;
      params.stu_id = $.cookie('indiv_id');
      return params;
    },
    columns: [{
      field: "cour_id",
      title: "课程id"
    }, {
      field: "title",
      title: "课程名称"
    }, {
      field: "teache_name",
      title: "任课教师"
    }, {
      field: "credit",
      title: "学分"
    }, {
      field: "hour",
      title: "学时"
    }, {
      field: "attribute",
      title: "课程类型",
      formatter: attributeFormatter,
    }, {
      field: 'cour_id',
      title: '可供操作',
      width: 300,
      align: 'center',
      valign: 'middle',
      formatter: unChooseFormatter,
    },]
  });
}

//生成已选择课程表格
function initHaveChooseCourse() {
  $un_choose_table = $('#student-choose-warp').bootstrapTable({
    url:"cotro/student/haveChooseCourse.ashx",
    method:"post",
    //data: have_choose_testData.rows,
    search: true,
    sidePagination: "client",
    pageList: [5, 25, 50, 100],
    pagination: true,
    height: 400,
    singleSelect: false,
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    queryParams: function (params) {
      //params.stu_id = 10012;
      params.stu_id = $.cookie('indiv_id');
      return params;
    },

    columns: [{
      field: "cour_id",
      title: "课程id"
    }, {
      field: "title",
      title: "课程名称"
    }, {
      field: "teache_name",
      title: "任课教师"
    }, {
      field: "credit",
      title: "学分"
    }, {
      field: "hour",
      title: "学时"
    }, {
      field: "attribute",
      title: "课程类型",
      formatter: attributeFormatter,
    }, {
      field: 'cour_id',
      title: '可供操作',
      width: 300,
      align: 'center',
      valign: 'middle',
      formatter: haveChooseFormatter
    },]
  });
}

function attributeFormatter(value, row, index) {
  return value = "1" ? "公选课" : "专选课";
}

function unChooseFormatter(value, row, index) {
  let result = `
        <button type="button" class="btn btn-success" onclick="detailedCourse(${row.cour_id})">详细信息</button>
        <button type="button" class="btn btn-primary" onclick="addCourse(${row.cour_id})">选课</button>
    `;
  return result;
}

function haveChooseFormatter(value, row, index) {
  let result = `
        <button type="button" class="btn btn-success" onclick="detailedCourse(${row.cour_id})">详细信息</button>
        <button type="button" class="btn btn-danger" onclick="deleteCourse(${row.cour_id})">退课</button>
    `;

  return result;
}

//单个课程的信息
function detailedCourse(cour_id) {

}

//选课
function addCourse(cour_id) {
  console.log("stu_id: " + $.cookie('indiv_id'));
  $.post("cotro/student/addChooseCourse.ashx", {
    stu_id: $.cookie('indiv_id'),
    cour_id: cour_id,
  }, function (res, status) {
    res=JSON.parse(res);
    if (res.line == "1") {
      alert("选课成功!");
      $un_choose_table.bootstrapTable('refresh');
      $have_choose_table.bootstrapTable('refresh');
    } else {
      alert("选课失败!");
    }
  });
}

//退课
function deleteCourse(cour_id) {
  console.log("stu_id: " + $.cookie('indiv_id'));
  $.post("cotro/student/deleteChooseCourse.ashx", {
    stu_id: $.cookie('indiv_id'),
    cour_id: cour_id,
  }, function (res, status) {
    res=JSON.parse(res);
    if (res.line == "1") {
      alert("退课成功!");
      $un_choose_table.bootstrapTable('refresh');
      $have_choose_table.bootstrapTable('refresh');
    } else {
      alert("退课失败!");
    }
  });
}